<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\article\faq;

use think\facade\Db;
use app\common\dao\article\faq\FaqDao;
use app\common\repositories\BaseRepository;
use app\common\repositories\property\users\PropertyUsersLogRepository;
use app\common\repositories\system\upload\SystemUploadFileRepository;

/**
 * Class FaqRepository
 * @package app\common\repositories\article\news
 * @mixin FaqDao
 */
class FaqRepository extends BaseRepository
{

    public function __construct(FaqDao $dao)
    {
        $this->dao = $dao;
    }

    public function getPropertyList(array $where, $page, $limit, int $propertyId = null)
    {
        $query = $this->dao->search($where, $propertyId);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->with([
                'cateInfo' => function ($query) {
                    $query->bind(['name']);
                },
                'fileInfo' => function ($query) {
                    $query->bind(['picture' => 'show_src']);
                }
            ])
            ->field('id,cate_id,title,picture_id,content,is_show,is_top,click,content_type,add_time')
            ->order('id', 'desc')
            ->select();
        return compact('count', 'list');
    }

    public function getApiList(array $where, $page, $limit, int $propertyId = null)
    {
        $query = $this->dao->search($where, $propertyId);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->with([
                'cateInfo' => function ($query) {
                    $query->field(['id, name']);
                    $query->bind(['cate_name' => 'name']);
                },
                'fileInfo' => function ($query) {
                    $query->bind(['picture' => 'show_src']);
                }
            ])
            ->field('id,cate_id,title,content,add_time,picture_id,click,network_url,content_type')
            ->hidden(['fileInfo', 'cateInfo', 'picture_id'])
            ->select();
        return compact('list', 'count');
    }

    public function addInfo(array $data, int $propertyId = 1, int $userId = 0)
    {
        /** @var SystemUploadFileRepository $uploadFileRepository */
        $uploadFileRepository = app()->make(SystemUploadFileRepository::class);
        $fileInfo = $uploadFileRepository->getFileData($data['picture']);
        if ($fileInfo) {
            $data['picture_id'] = $fileInfo['id'];
        }
        unset($data['picture']);
        return Db::transaction(function () use ($data, $propertyId, $userId) {
            $data['property_id'] = $propertyId;
            $res = $this->dao->create($data);
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($propertyId, $userId, 'ARTICLE.FAQ.ADD', $data);
            return $res;
        });
    }

    public function editInfo($info, array $data, int $userId = 0)
    {
        if ($data['picture']) {
            /** @var SystemUploadFileRepository $uploadFileRepository */
            $uploadFileRepository = app()->make(SystemUploadFileRepository::class);
            $fileInfo = $uploadFileRepository->getFileData($data['picture']);
            if ($fileInfo) {
                if ($fileInfo['id'] != $info['picture_id']) {
                    $data['picture_id'] = $fileInfo['id'];
                }
            }
        }
        unset($data['picture']);
        return Db::transaction(function () use ($data, $info, $userId) {
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($info['property_id'], $userId, 'ARTICLE.FAQ.EDIT', $data);
            return $this->dao->update($info['id'], $data);
        });
    }


    public function getDetail(int $id)
    {
        $data = $this->dao->search([])
            ->with([
                'fileInfo' => function ($query) {
                    $query->bind(['picture' => 'show_src']);
                }
            ])
            ->hidden(['fileInfo'])
            ->where('id', $id)
            ->find();
        $data['content'] = htmlspecialchars_decode($data['content']);
        return $data;
    }

    public function getApiDetail(int $id)
    {
        $data = $this->dao->getSearch([])
            ->with([
                'cateInfo' => function ($query) {
                    $query->field(['id, name']);
                    $query->bind(['cate_name' => 'name']);
                },
                'fileInfo' => function ($query) {
                    $query->bind(['picture' => 'show_src']);
                }
            ])
            ->field('id,cate_id,title,content,add_time,picture_id,click')
            ->hidden(['fileInfo', 'cateInfo', 'picture_id'])
            ->where('id', $id)
            ->where('is_show', 1)
            ->find();
        $data['content'] = htmlspecialchars_decode($data['content']);
        return $data;
    }

    public function batchDelete(array $ids, int $userId = 0)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ]);
        if ($list) {
            foreach ($list as $k => $v) {
                $this->deleteInfo($v, $userId);
            }
            return $list;
        }
        return [];
    }

    public function deleteInfo($info, int $userId = 0)
    {
        /** @var PropertyUsersLogRepository $repository */
        $repository = app()->make(PropertyUsersLogRepository::class);
        $repository->addInfo($info['property_id'], $userId, 'ARTICLE.FAQ.DEL', $info);
        $this->dao->delete($info['id']);
    }

}